package cn.fotoit.product.system.service;

import cn.fotoit.product.system.entity.User;
import cn.fotoit.product.system.repository.UserRepository;
import cn.fotoit.product.util.BeanUtil;
import cn.fotoit.product.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;


@Service
@CacheConfig(cacheNames = "queue_management_userService")
@Transactional(readOnly = true)
public class UserService {
    @Autowired
    private UserRepository userRepository;

    @CacheEvict(allEntries = true, cacheNames = {"queue_management_permissionService", "queue_management_roleRelPermissionService", "queue_management_roleService", "queue_management_userService"})
    @Transactional
    public void add(User user) {
        userRepository.save(user);
    }

    @CacheEvict(allEntries = true, cacheNames = {"queue_management_permissionService", "queue_management_roleRelPermissionService", "queue_management_roleService", "queue_management_userService"})
    @Transactional
    public void deleteById(Integer id) {
        userRepository.deleteById(id);
    }

    @CacheEvict(allEntries = true, cacheNames = {"queue_management_permissionService", "queue_management_roleRelPermissionService", "queue_management_roleService", "queue_management_userService"})
    @Transactional
    public void updateNotNull(User user) {
        Optional<User> byId = userRepository.findById(user.getId());
        if (byId.isPresent()) {
            User obj = byId.get();
            BeanUtil.copyPropertiesIgnoreNull(user, obj);
            userRepository.save(obj);
        }
    }

    @Cacheable(key = "'findAll'+page+'size'+#size")
    public Page<User> findAll(Integer page, Integer size) {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        return userRepository.findAll(pageable);
    }

    @Cacheable(key = "'findById'+#id")
    public User findById(Integer id) {
        Optional<User> byId = userRepository.findById(id);
        if (byId.isPresent()) {
            return byId.get();
        }
        return null;
    }

    @Cacheable(key = "'findByLoginName'+#loginName")
    public User findByLoginName(String loginName) {
        Optional<User> byId = userRepository.findByLoginName(loginName);
        if (byId.isPresent()) {
            return byId.get();
        }
        return null;
    }

    //如果存在返回false
    public Boolean checkLoginName(String loginName) {
        Optional<User> byId = userRepository.findByLoginName(loginName);
        if (byId.isPresent()) {
            return false;
        }
        return true;
    }

    @CacheEvict(allEntries = true,cacheNames = {"queue_management_permissionService","queue_management_roleRelPermissionService","queue_management_roleService","queue_management_userService"})
    @Transactional
    public void updatePasswordById(Integer id, String loginPassword) {
        Optional<User> byId = userRepository.findById(id);
        if (byId.isPresent()) {
            User user = byId.get();
            user.setLoginPassword(PasswordUtil.getPasswordByUsernameAndPassword(user.getLoginName(), loginPassword));
        }

    }
}